﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;

using System.IO;
using System.Net;
using System.Net.Cache;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;


namespace LCWMS
{
    public partial class  w_mstsyn : DockContent
    {
        DataSet dscustomer;
        DataSet ds_tran;
        Cursor currentCursor = Cursor.Current;

        public w_mstsyn()
        {
            InitializeComponent();
            
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {

            if (!chkBox_syncSku.Checked && !chkBox_syncLine.Checked && !chkBox_syncLab.Checked && !chkBox_syncPick.Checked && !chkBox_syncRev.Checked && !chkBox_syncSend.Checked && !chkBox_syncShopRet.Checked && !chkBox_syncWareRet.Checked)
            {

                MessageBox.Show("至少选择一个数据同步项目");
                return;
            }
           
            // 设置为忙碌状态
            Cursor.Current = Cursors.WaitCursor;
            btn_Query.Enabled = false;

            dataSync();

            btn_Query.Enabled = true;
            Cursor.Current = currentCursor;

            MessageBox.Show("数据同步结束!");
        }

        //数据同步
        private void dataSync() {

            try
            {
                //1）商品数据同步
                if (chkBox_syncSku.Checked)
                {
                    syncSkuFunction();
                }
                //1.1）线路数据同步
                if (chkBox_syncLine.Checked)
                {
                    syncLineFunction();
                }
                //2）标签数据同步
                if (chkBox_syncLab.Checked)
                {
                    syncLabFunction();
                }
                //3）拣货数据同步
                if (chkBox_syncPick.Checked)
                {
                    syncPickFunction();
                }
                //4）库存数据同步
                if (chkBox_syncInv.Checked)
                {
                   //http://8.130.126.138:8057/scheduleHYStockATask
                }

                //5）收货数据同步
                if (chkBox_syncRev.Checked)
                {
                    syncRevFunction();
                }

                //6）发货数据同步
                if (chkBox_syncSend.Checked)
                {
                    syncSendFunction();
                }

                //7）门店退大仓数据同步
                if (chkBox_syncShopRet.Checked)
                {
                    syncShopFunction();
                }

                //8）大仓退供应商数据同步
                if (chkBox_syncWareRet.Checked)
                {
                    syncWareFunction();
                }

            }
            catch (System.Exception ee)
            {

                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }
        
        
        }


        private void w_mstsyn_Load(object sender, EventArgs e)
        {

            //收货数据
            dtp_rev_time.CustomFormat = "yyyy.MM.dd";
            dtp_rev_time.Format = DateTimePickerFormat.Custom;
            dtp_rev_time.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(-5).ToString("yyyy.MM.dd").Trim());

            dtp_rev_endtime.CustomFormat = "yyyy.MM.dd";
            dtp_rev_endtime.Format = DateTimePickerFormat.Custom;
            dtp_rev_endtime.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(-3).ToString("yyyy.MM.dd").Trim());

            //发货数据
            dtp_send_time.CustomFormat = "yyyy.MM.dd";
            dtp_send_time.Format = DateTimePickerFormat.Custom;
            dtp_send_time.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(-5).ToString("yyyy.MM.dd").Trim());

            dtp_send_endtime.CustomFormat = "yyyy.MM.dd";
            dtp_send_endtime.Format = DateTimePickerFormat.Custom;
            dtp_send_endtime.Value  = System.Convert.ToDateTime(DateTime.Now.AddDays(-3).ToString("yyyy.MM.dd").Trim());

            //门店退大仓
            dtp_shop_time.CustomFormat = "yyyy.MM.dd";
            dtp_shop_time.Format = DateTimePickerFormat.Custom;
            dtp_shop_time.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(-5).ToString("yyyy.MM.dd").Trim());

            dtp_shop_endtime.CustomFormat = "yyyy.MM.dd";
            dtp_shop_endtime.Format = DateTimePickerFormat.Custom;
            dtp_shop_endtime.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(-3).ToString("yyyy.MM.dd").Trim());

            //大仓退门店
            dtp_ware_time.CustomFormat = "yyyy.MM.dd";
            dtp_ware_time.Format = DateTimePickerFormat.Custom;
            dtp_ware_time.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(-5).ToString("yyyy.MM.dd").Trim());

            dtp_ware_endtime.CustomFormat = "yyyy.MM.dd";
            dtp_ware_endtime.Format = DateTimePickerFormat.Custom;
            dtp_ware_endtime.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(-3).ToString("yyyy.MM.dd").Trim());

          
        }


        //商品数据同步
        public string syncSkuFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/masterTask";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }

        //线路数据同步
        public string syncLineFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/api/inLineTask";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }

        //标签数据同步
        public string syncLabFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/schedulelnPickPrintLablelnfoTask";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }

        //标签数据同步
        public string syncPickFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/schedulelnOrderPickStsTask";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }

        //dtp_h_time.Text.ToString().Trim()

        //收货数据同步
        public string syncRevFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/api/inAsnTask?startDate=" + dtp_rev_time.Text.ToString().Trim().Replace(".", "") + "&endDate=" + dtp_rev_endtime.Text.ToString().Trim().Replace(".", "");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }

        //发货数据同步
        public string syncSendFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/api/inSoTask?startDate=" + dtp_send_time.Text.ToString().Trim().Replace(".", "") + "&endDate=" + dtp_send_endtime.Text.ToString().Trim().Replace(".", "");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }

        //门店退大仓数据同步
        public string syncShopFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/api/getStoreToDcApply?startDate=" + dtp_shop_time.Text.ToString().Trim().Replace(".", "") + "&endDate=" + dtp_shop_endtime.Text.ToString().Trim().Replace(".", "");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }

        ////大仓退门店数据同步
        public string syncWareFunction()
        {
            try
            {
                string[] sUrlArr = main.g_server.Split(':');
                if (sUrlArr.Length == 0)
                    return "";

                string sUrl = "http://" + sUrlArr[0] + ":8057/api/getDcToSupplierApply?startDate=" + dtp_ware_time.Text.ToString().Trim().Replace(".", "") + "&endDate=" + dtp_ware_endtime.Text.ToString().Trim().Replace(".", "");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;

            }
            catch (Exception ex)
            {
                return "";
            }
        }
      
    }
}